package com.example.controller;

import com.example.dto.LoginDto;
import com.example.dto.RegisterDto;
import com.example.pojo.User;
import com.example.service.UserService;
import com.example.service.impl.UserServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.http.HttpStatus;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/users")
public class UserController {
    @Autowired
    private UserService userService;

    /**
     * 用户登录
     * @param loginDto 包含用户ID和密码的登录数据传输对象
     * @return 登录成功时返回包含用户信息的响应,登录失败时返回错误信息
     */
    @PostMapping("/login")
    public ResponseEntity<Map<String, Object>> login(@RequestBody LoginDto loginDto) {
        try {
            User user = userService.login(loginDto.getUserId(), loginDto.getPassword());
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "登录成功");
            response.put("user", user);
            return ResponseEntity.ok(response);
        } catch (UserServiceImpl.AccountNotFoundException e) {
            return ResponseEntity.status(HttpStatus.OK).body(Map.of("code", 600, "msg", e.getMessage()));
        } catch (UserServiceImpl.IncorrectPasswordException e) {
            return ResponseEntity.status(HttpStatus.OK).body(Map.of("code", 800, "msg", e.getMessage()));
        } catch (RuntimeException e) {
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Map.of("code", 400, "msg", e.getMessage()));
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("msg", "登录失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }

    /**
     * 用户注册
     * @param registerDto 包含注册信息的数据传输对象
     * @return 注册成功时返回成功信息,注册失败时返回错误信息
     */
    @PostMapping("/register")
    public ResponseEntity<Map<String, Object>> register(@RequestBody RegisterDto registerDto) {
        try {
            User registeredUser = userService.register(registerDto);
            Map<String, Object> response = new HashMap<>();
            response.put("code", 200);
            response.put("msg", "注册成功");
            response.put("user", registeredUser);
            return ResponseEntity.ok(response);
        } catch (IllegalArgumentException e) {
            if ("账户已存在".equals(e.getMessage())) {
                return ResponseEntity.status(HttpStatus.OK).body(Map.of("code", 600, "msg", e.getMessage()));
            }
            return ResponseEntity.status(HttpStatus.BAD_REQUEST).body(Map.of("code", 400, "msg", e.getMessage()));
        } catch (Exception e) {
            Map<String, Object> response = new HashMap<>();
            response.put("code", 500);
            response.put("msg", "注册失败：" + e.getMessage());
            return ResponseEntity.status(HttpStatus.INTERNAL_SERVER_ERROR).body(response);
        }
    }
}
